New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

@id-sdk/vector

Package Overview
Dependencies
Maintainers
2
Versions
7
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@id-sdk/vector

Vector (coordinate) math functions

  • 3.0.0-pre.10
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
75
increased by27.12%
Maintainers
2
Weekly downloads
 
Created
Source

npm version

@id-sdk/vector

📐 Vector (coordinate) math functions

Installing

npm install @id-sdk/vector

This library is distributed in ESM format only. It cannot be require()'d from CommonJS. For more, please read Sindre Sorhus’s FAQ.

import * as vec from '@id-sdk/vector';
import { vecEqual } from '@id-sdk/vector';

Contributing

This project is just getting started! 🌱

We're not able to support external contributors at this time, but check back in a bit when things have matured.

API Reference

Functions
Types
  • Vec2: [number, number]
  • Edge: { index: number, distance: number, target: Vec2 }

Functions

# vecEqual(a: Vec2, b: Vec2, epsilon?: number): boolean <>

Test whether two given vectors are equal (optionally, to within epsilon).

vecEqual([1, 2], [1, 2]);                         // returns true
vecEqual([1, 2], [1.0000001, 2.0000001], 1e-5);   // returns true

# vecAdd(a: Vec2, b: Vec2): Vec2 <>

Adds two vectors, returns the vector sum of a + b.

vecAdd([1, 2], [3, 4]);   // returns [4, 6]

# vecSubtract(a: Vec2, b: Vec2): Vec2 <>

Subtracts two vectors, returns the vector difference of a - b.

vecSubtract([1, 2], [3, 4]);   // returns [-2, -2]

# vecScale(a: Vec2, n: number): Vec2 <>

Scale a vector uniformly by factor, returns the scaled vector.

vecScale([1, 2], 2);   // returns [2, 4]

# vecFloor(a: Vec2): Vec2 <>

Round down the coordinates of a vector.

vecFloor([0, 1.01]);   // returns [0, 1]

# vecInterp(a: Vec2, b: Vec2, t: number): Vec2 <>

Linear interpolate a point along a vector.

vecInterp([0, 0], [10, 10], 0.5);   // returns [5, 5]

# vecLength(a: Vec2, b?: Vec2): number <>

Returns the length of a vector. If b is not passed in, it defaults to [0,0].

vecLength([0, 0], [4, 3]);   // returns 5
vecLength([4, 3]);           // returns 5

# vecNormalize(a: Vec2): Vec2 <>

Normalize a vector (i.e. returns a unit vector).

vecNormalize([5, 0]);   // returns [1, 0]

# vecAngle(a: Vec2, b: Vec2): number <>

Returns the counterclockwise angle in the range (-pi, pi) between the positive X axis and the line intersecting a and b.

vecAngle([0, 0], [-1, 0]);   // returns π

# vecDot(a: Vec2, b: Vec2, origin?: Vec2): number <>

Returns the dot product of two vectors. If origin is not passed in, it defaults to [0,0].

vecDot([2, 0], [2, 0]);   // returns 4

# vecNormalizedDot(a: Vec2, b: Vec2, origin?: Vec2): number <>

Normalized Dot Product - normalizes input vectors before returning dot product. If origin is not passed in, it defaults to [0,0].

vecNormalizedDot([2, 0], [2, 0]);   // returns 1

# vecCross(a: Vec2, b: Vec2, origin?: Vec2): number <>

Returns the 2D cross product of OA and OB vectors, returns magnitude of Z vector. If origin is not passed in, it defaults to [0,0].

This returns a positive value, if OAB makes a counter-clockwise turn, negative for clockwise turn, and zero if the points are collinear.

vecCross([2, 0], [0, 2]);   // returns 4

# vecProject(a: Vec2, points: Vec2[]): Edge | null <>

Find closest orthogonal projection of point onto points array. Returns an Edge object containing info about the projected point, or null if points is a degenerate path (0- or 1- point).

//     c
//     |
// a --*--- b
//
// * = [2, 0]
//
const a = [0, 0];
const b = [5, 0];
const c = [2, 1];
vecProject(c, [a, b]);   // returns Edge { index: 1, distance: 1, target: [2, 0] }

Types

# Vec2

An array of two numbers.

[number, number]

# Edge

An Object containing index, distance, and target properties. Used as the return value for vecProject().

{ index: number, distance: number, target: Vec2 }

FAQs

Package last updated on 10 Sep 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc